package com.anbinh.doctor.fragment.account;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.model.CityResponse;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.DistrictResponse;
import com.anbinh.doctor.model.ProfileResponse;
import com.anbinh.doctor.model.ProfileResponse.Data.Profile;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;
import com.zcw.togglebutton.ToggleButton;
import com.zcw.togglebutton.ToggleButton.OnToggleChanged;

public class FragmentAccountInfo extends Fragment {
	private static FragmentAccountInfo fragment = null;
	private View mView;
	private View mLoadding;
	private UserLogin userLogin;
	private TextView lblTinhthanh;
	private TextView lblQuanhuyen;
	private TextView lblGender;
	private TextView lblNgaysinh;

	private View spTinhThanh;
	private View spQuanHuyen;
	private int mCity_id = 1;
	private int mDistrict_id = 1;
	private String mBrithday = "";
	private int mGender = 1;

	private EditText lblName;
	private EditText lblEmail;
	private Profile profile;
	private ToggleButton gender;
	protected DistrictResponse currentDistrict;
	private View spNgaysinh;
	private View viewsdt;

	public static FragmentAccountInfo newInstance() {
		if (fragment == null) {
			fragment = new FragmentAccountInfo();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_account_info, null);
		Utils.setOnTouchHideKeyboard(getActivity(), mView);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		viewsdt=(View)mView.findViewById(R.id.viewsdt);
		viewsdt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Utils.showMessage(getActivity(), "Số điện thoại không được thay đổi");
			}
		});
		
		gender = (ToggleButton) mView.findViewById(R.id.gengerToggle);
		mLoadding = (View) mView.findViewById(R.id.loadding);
		spTinhThanh = (View) mView.findViewById(R.id.spTinhthanh);
		spTinhThanh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				CityResponse cityResponse = Utils.getCityResponse(
						getActivity(), Utils.KEY_CITY);
				if (cityResponse == null) {
					onLoadCity();
				} else {
					getSelectedCity(cityResponse);
				}

			}
		});

		spQuanHuyen = (View) mView.findViewById(R.id.spQuanHuyen);
		spQuanHuyen.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onSelectDistrict();
			}
		});
		lblNgaysinh = (TextView) mView.findViewById(R.id.lblNgaysinh);
		spNgaysinh = (View) mView.findViewById(R.id.spNgaysinh);
		spNgaysinh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DialogFragment newFragment = new DatePickerFragment(lblNgaysinh);
				newFragment.show(getFragmentManager(), "datePicker");
			}
		});

		gender.setOnToggleChanged(new OnToggleChanged() {

			@Override
			public void onToggle(boolean on) {
				if (on) {
					lblGender.setText("Nam");
					mGender = 1;
				} else {
					mGender = 0;
					lblGender.setText("Nữ");
				}
				updateInfo();
			}
		});

		lblName = (EditText) mView.findViewById(R.id.lblName);

		final TextView lblPhone = (TextView) mView.findViewById(R.id.lblPhone);
		lblEmail = (EditText) mView.findViewById(R.id.lblEmail);
		lblGender = (TextView) mView.findViewById(R.id.lblGender);
		lblTinhthanh = (TextView) mView.findViewById(R.id.lblTinhthanh);
		lblQuanhuyen = (TextView) mView.findViewById(R.id.lblChuyenKhoaName);

		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
		if (userLogin == null)
			return;
		mLoadding.setVisibility(View.VISIBLE);
		ServerController.getInstance(getActivity()).requestGetProfile(
				userLogin.getData().getToken(),
				new OnRequest<ProfileResponse>() {

					@Override
					public void onSuccess(ProfileResponse data) {
						mLoadding.setVisibility(View.INVISIBLE);
						if (data.getResponsecode() >= 0) {
							profile = data.getData().getProfile();
							mCity_id = profile.getCity_id();
							lblName.setText(profile.getName());
							if (profile.getGender().equalsIgnoreCase("Nam")) {
								gender.setToggleOn(true);
								mGender = 1;
							} else {
								mGender = 0;
								gender.setToggleOff(true);
							}

							lblPhone.setText(profile.getPhone());
							lblEmail.setText(profile.getEmail());
							lblGender.setText(profile.getGender());
							lblTinhthanh.setText(profile.getCity());
							lblQuanhuyen.setText(profile.getDistrict());
							lblNgaysinh.setText(profile.getBirth_date());

						} else {
							Utils.showMessage(getActivity(),
									data.getDescription());
						}
					}

					@Override
					public void onError(int errorCode, String message) {
						mLoadding.setVisibility(View.INVISIBLE);
					}
				});

	}

	protected void onSelectDistrict() {
		if(mCity_id==-1){
			Utils.showMessage(getActivity(), "Vui lòng chọn Tỉnh/T.Phố trước");
			return;
		}
		
		if (currentDistrict != null) {
			
			Utils.showDropdowDistrict(getActivity(), currentDistrict.getData()
					.getDistrict(), new IRcallBak() {

				@Override
				public void onDisplay(int type, Bundle data) {
					mDistrict_id = type;
					lblQuanhuyen.setText(data.getString(Utils.KEY_TINHTHANH));
				}
			}, "Chọn quận huyện");
		} else {
			onLoadDistrict(true);
		}
	}

	protected void getSelectedCity(CityResponse cityResponse) {
		Utils.showDropdow(getActivity(), cityResponse.getData().getCity(),
				new IRcallBak() {

					@Override
					public void onDisplay(int type, Bundle data) {
						mCity_id = type;
						
						lblTinhthanh.setText(data
								.getString(Utils.KEY_TINHTHANH));
						onLoadDistrict(false);

					}
				}, "Chọn thành phố");

	}

	public void onLoadCity() {
		mLoadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);

		ServerController.getInstance(getActivity()).requestCity(
				userLogin.getData().getToken(), new OnRequest<CityResponse>() {

					@Override
					public void onSuccess(CityResponse data) {
						Utils.updateData(getActivity(), Utils.KEY_CITY, data);
						mLoadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
						getSelectedCity(data);

					}

					@Override
					public void onError(int errorCode, String message) {
						mLoadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);

					}

				});
	}

	public static class DatePickerFragment extends DialogFragment implements
			DatePickerDialog.OnDateSetListener {
		TextView textView;

		public DatePickerFragment(TextView textView) {
			this.textView = textView;
		}

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			// Use the current date as the default date in the picker
			final Calendar c = Calendar.getInstance();
			int year = c.get(Calendar.YEAR);
			int month = c.get(Calendar.MONTH);
			int day = c.get(Calendar.DAY_OF_MONTH);

			// Create a new instance of DatePickerDialog and return it
			return new DatePickerDialog(getActivity(), this, year, month, day);
		}

		public void onDateSet(DatePicker view, int year, int month, int day) {
			month++;
			textView.setText(year + "-" + month + "-" + day);
		}
	}

	public void onLoadDistrict(final boolean isShow) {
		mLoadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);

		ServerController.getInstance(getActivity()).requestDistrict(
				userLogin.getData().getToken(), mCity_id,
				new OnRequest<DistrictResponse>() {

					@Override
					public void onSuccess(DistrictResponse data) {
						if (getActivity() == null)
							return;
						currentDistrict = data;
						mLoadding.setVisibility(View.INVISIBLE);
						if (data.getResponsecode() < 0)
							return;
						if (isShow) {
							onSelectDistrict();
						} else {
							lblQuanhuyen.setText(currentDistrict.getData()
									.getDistrict().get(0).getName());
							mDistrict_id = currentDistrict.getData()
									.getDistrict().get(0).getId();
						}
						Utils.disableEnableControls(true, mView);

					}

					@Override
					public void onError(int errorCode, String message) {
						mLoadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);

					}

				});
	}

	public void updateInfo() {
		mLoadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);
		final String doctor_name = lblName.getText().toString().trim();
		String email = lblEmail.getText().toString().trim();
		mBrithday = lblNgaysinh.getText().toString().trim();

		ServerController.getInstance(getActivity()).requestUpdateProfile(
				userLogin.getData().getToken(), doctor_name, email, mGender,
				mCity_id, mDistrict_id, mBrithday,
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						mLoadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
						Utils.showMessage(getActivity(), data.getDescription());
						if (data.getResponsecode() >= 0) {
							userLogin.getData().getUserInfo()
									.setFull_name(doctor_name);
							Utils.updateData(getActivity(), Utils.KEY_USER,
									userLogin);
						}
					}

					@Override
					public void onError(int errorCode, String message) {
						mLoadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
					}
				});
	}

}
